package com.wuwu.springcloud.controller;

import com.wuwu.springcloud.pojo.Dept;
import com.wuwu.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {
    @Autowired
    //注册 restTemplate
    // （ url , 实体 ：map , class<T> responseType ）
  private RestTemplate restTemplate; //提供 多种 便捷访问远程http的方法   简单rest服务模板
    //消费者
    //restful 远程 拿到 路径实现连接   并解耦
//    private static final String  REST_URL_PREFIX = "http://localhost:8081";
    private static final String  REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

//


    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        // 路径+ dept +  返回值 类型
         return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class) ;
    }
    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        // 路径+ dept + 返回值类
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,boolean.class) ;
    }
    @RequestMapping("/consumer/dept/list")
    public List<Dept> query(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class) ;
    }

}
